/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.persistence.hibernate.core;

import com.blandware.atleap.common.util.PartialCollection;
import com.blandware.atleap.common.util.QueryInfo;
import com.blandware.atleap.model.core.ContentLocale;
import com.blandware.atleap.persistence.core.ContentLocaleDAO;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>Hibernate implementation of ContentLocaleDAO</p>
 * <p><a href="ContentLocaleDAOHibernate.java.html"><i>View Source</i></a></p>
 *
 * @author Andrey Grebnev <a href="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.29 $ $Date: 2008/06/23 13:24:53 $
 */
public class ContentLocaleDAOHibernate extends BaseDAOHibernate implements ContentLocaleDAO {

	/**
	 * Creates new instance of ContentLocaleDAOHibernate
	 */
	public ContentLocaleDAOHibernate() {
	}

	/**
	 * @see com.blandware.atleap.persistence.core.ContentLocaleDAO#createContentLocale(com.blandware.atleap.model.core.ContentLocale)
	 */
	public void createContentLocale(ContentLocale contentLocale) {
		getHibernateTemplate().save(contentLocale);
	}

	/**
	 * @see com.blandware.atleap.persistence.core.ContentLocaleDAO#retrieveContentLocale(java.lang.String)
	 */
	public ContentLocale retrieveContentLocale(String localeIdentifier) {
		return (ContentLocale) getHibernateTemplate().get(ContentLocale.class, localeIdentifier);
	}

	/**
	 * @see com.blandware.atleap.persistence.core.ContentLocaleDAO#updateContentLocale(com.blandware.atleap.model.core.ContentLocale)
	 */
	public void updateContentLocale(ContentLocale contentLocale) {
		getHibernateTemplate().update(contentLocale);
	}

	/**
	 * @see com.blandware.atleap.persistence.core.ContentLocaleDAO#deleteContentLocale(com.blandware.atleap.model.core.ContentLocale)
	 */
	public void deleteContentLocale(ContentLocale contentLocale) {
		getHibernateTemplate().delete(contentLocale);
	}

	/**
	 * @see com.blandware.atleap.persistence.core.ContentLocaleDAO#listContentLocales(com.blandware.atleap.common.util.QueryInfo)
	 */
	public PartialCollection listContentLocales(QueryInfo queryInfo) {
		String whereClause = new String();
		String orderByClause = new String();
		if ( queryInfo != null ) {
			whereClause = queryInfo.getWhereClause();
			orderByClause = queryInfo.getOrderByClause();
			if ( whereClause != null && whereClause.length() != 0 ) {
				whereClause = " where " + whereClause;
			} else {
				whereClause = new String();
			}
			if ( orderByClause != null && orderByClause.length() != 0 ) {
				orderByClause = " order by " + orderByClause;
			} else {
				orderByClause = " order by l.identifier";
			}
		}

		List list = null;
		Integer total = null;
		String hqlPart = "from ContentLocale l" + whereClause;
		if ( queryInfo != null && (queryInfo.getLimit() != null || queryInfo.getOffset() != null) ) {
			String hqlForTotal = "select count(l.identifier) " + hqlPart;
			total = findUniqueIntegerResult(hqlForTotal);
			if ( total == null ) {
				total = new Integer(0);
			}
		}
        // If we don't have any info about the total number of results yet or
        // we know that there's something that will be found, then fetch data
		if ( total == null || total.intValue() > 0 ) {
			String hql = "select l " + hqlPart + orderByClause;
			list = executeFind(hql, queryInfo);
			if ( total == null ) {
				total = new Integer(list.size());
			}
		} else {
			list = new ArrayList();
		}
		return new PartialCollection(list, total);
	}

	/**
	 * @see com.blandware.atleap.persistence.core.ContentLocaleDAO#findDefaultContentLocale()
	 */
	public ContentLocale findDefaultContentLocale() {
		return (ContentLocale) findUniqueResult("from ContentLocale l where l.defaultInstance = 'T'", true, "query.findDefaultContentLocale");
	}

}
